package a66weding.com.jiehuntong.activity.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MeSetNewPasswordBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.WXbean;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * 我的-设置-账号安全-设置登录密码界面
 */
public class MeSetNewpasswordActivity extends MeBaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, OnNetResultListener {
    private LinearLayout mBackLay;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private RelativeLayout mHeaderRightRlay;
    private Button mCommitBtn;
    private EditText mOldPassword;
    private EditText mNewPassword;
    private EditText mVerifyPassword;
    private String oldPassword;
    private String newPassword;
    private String verifyPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_set_newpassword);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("MeSetNewpasswordActivity", this);

        initview();
    }

    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackContentTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mHeaderRightRlay = (RelativeLayout) findViewById(R.id.rlay_header_rightfunc);
        mCommitBtn = (Button) findViewById(R.id.btn_commit);
        mOldPassword = (EditText) findViewById(R.id.et_meset_oldpassword);
        mNewPassword = (EditText) findViewById(R.id.et_meset_newpassword);
        mVerifyPassword = (EditText) findViewById(R.id.et_meset_renewpassword);

        mBackContentTv.setText("返回");
        mHeaderTitleTv.setText("设置密码");
        mHeaderRightRlay.setVisibility(View.GONE);

        mBackLay.setOnClickListener(this);
        mCommitBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llay_header_back:     //点击返回按钮，关闭当前页
                finish();

                break;
            case R.id.btn_commit:
                oldPassword = mOldPassword.getText().toString().trim();
                newPassword = mNewPassword.getText().toString().trim();
                verifyPassword = mVerifyPassword.getText().toString().trim();
                if (TextUtils.isEmpty(oldPassword)) {
                    CUtils.showToast("旧密码不能为空");
                    return;
                } else if (TextUtils.isEmpty(newPassword)) {
                    CUtils.showToast("新密码不能为空");
                    return;
                } else if (TextUtils.isEmpty(verifyPassword)) {
                    CUtils.showToast("确认密码不能为空");
                    return;
                } else if (!(newPassword.equals(verifyPassword))) {
                    CUtils.showToast("两次输入的新密码不同");
                    return;
                } else if (newPassword.length() < 6) {
                    CUtils.showToast("密码必须为6位！");
                    return;
                }
                // 通过服务器的get请求将本地的旧密码换成MD5的值
                String meOldPasswordUrl = Url.MEOLDPASSWORD;
                meOldPasswordUrl += oldPassword;
                HttpRequestBean oldPaddwordRequestBean = new HttpRequestBean(RequestCode.GETREQUEST,
                        meOldPasswordUrl, this, MeSetNewPasswordBean.class, RequestCode.OLDPASSWORD);
                HttpHandleUtil.executeCommonGet(MeSetNewpasswordActivity.this, oldPaddwordRequestBean);

                break;
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        MeSetNewPasswordBean meSetNewPasswordBean = null;
        if (data instanceof MeSetNewPasswordBean) {
            meSetNewPasswordBean = (MeSetNewPasswordBean) data;
            String password = DataWareHourse.getInstance().getmUser().getData().get(0).getNvc_PassWord();
            oldPassword = meSetNewPasswordBean.getResult();
            if (!(password.equals(oldPassword))) {
                Toast.makeText(MeSetNewpasswordActivity.this, "您输入的密码有误,请重新输入", Toast.LENGTH_SHORT).show();
                return;
            }
            // 通过post请求修改密码
            String url = UrlDealUtil.getRealUrl(RequestCode.UPDATE_PASSWORD);
            HashMap<String, String> requestParams = new HashMap<String, String>();
            requestParams.put("Password", "" + newPassword);
            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                    this, CommonResultBean.class, RequestCode.UPDATE_PASSWORD, requestParams);
            HttpHandleUtil.executeCommonPost(this, httpRequestBean);
        }else if(data instanceof CommonResultBean){
            CommonResultBean commondata= (CommonResultBean) data;
            Toast.makeText(MeSetNewpasswordActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
            // 通过服务器的get请求将本地的旧密码换成MD5的值
            String meOldPasswordUrl = Url.MEOLDPASSWORD;
            meOldPasswordUrl += newPassword;
            HttpRequestBean oldPaddwordRequestBean = new HttpRequestBean(RequestCode.GETREQUEST,
                    meOldPasswordUrl, this, WXbean.class, RequestCode.OLDPASSWORD);
            HttpHandleUtil.executeCommonGet(MeSetNewpasswordActivity.this, oldPaddwordRequestBean);
        } else if (data instanceof WXbean){
            WXbean wxbean = (WXbean) data;
            if ("0000".equals(wxbean.getResCode())){
                String meNewPassWord = wxbean.getResult();
                DataWareHourse.getInstance().getmUser().getData().get(0).setNvc_PassWord(meNewPassWord);
                finish();
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
        Toast.makeText(MeSetNewpasswordActivity.this, "网络请求失败,请稍后再试", Toast.LENGTH_SHORT).show();
    }
}
